package com.bookmarket.controller;

import com.bookmarket.entity.Book;
import com.bookmarket.service.CzgChaxunService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.annotation.Resource;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author Chen Zhaoguang
 * @date 2018/1/10 17:30
 */
@RestController
@RequestMapping("/chaxun")
public class CzgChaxunController {
    @Resource
    private CzgChaxunService czgChaxunService;

/*    //查询指定小类型下的图书
    @RequestMapping("/getBookByStype")
    public ModelAndView getBookByStype(int id){
        ModelAndView mv = new ModelAndView();
        List<Book> list = czgChaxunService.getBookByStype(id);
        mv.addObject("getStype",list);
        mv.setViewName("stage/LJproduct_list");
        return mv;
    }*/
    //查询补全
    @RequestMapping("/getBookByKeyword")
    public ModelAndView getBookByKeyword(String keyword){
        ModelAndView mv = new ModelAndView();
        List<String> list =null;
        if (keyword!=""){
           list = czgChaxunService.getBookByKeyword(keyword);
        }
        String result = "";
        if (list.size() > 0) {
            for (String title : list) {
                result += title + ",";
            }
            String list1= result.substring(0, result.length() - 1);
            mv.addObject("getKeyword",list1);
            mv.setView(new MappingJackson2JsonView());
            return mv;
        } else {
            return null;
        }
    }

    @RequestMapping("/getBookByKeyword2")
    public ModelAndView getBookByKeyword2(String keyword){
        ModelAndView mv = new ModelAndView();
        List<Book> list = czgChaxunService.getBookByKeyword2(keyword);
        mv.addObject("getKeyword",list);
        mv.setView(new MappingJackson2JsonView());
        return mv;
    }


    public CzgChaxunService getCzgChaxunService() {
        return czgChaxunService;
    }

    public void setCzgChaxunService(CzgChaxunService czgChaxunService) {
        this.czgChaxunService = czgChaxunService;
    }
}
